package com.frame.work.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.frame.work.common.util.Time;
import com.frame.work.entity.TblSensirionLog;
import com.frame.work.entity.dto.TblSensirionLogDTO;
import com.frame.work.service.TblSensirionLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wjming
 */
@RestController
@RequestMapping("/data/sensirion/log")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SensirionLogController {
    private final TblSensirionLogService sensirionLogService;

    /**
     * 通过时间段
     *
     * @return 单条数据List集合
     */
    @GetMapping("/by/time/slot")
    public R selectLog(@RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("deviceMac") String deviceMac,
                       @RequestParam("gatewayMac") String gatewayMac,
                       @RequestParam("start") long start,
                       @RequestParam("end") long end) {
        if (pageNum == 0) {
            pageNum = 0;
        } else {
            pageNum = (pageNum - 1) * pageSize;
        }
        pageSize = pageNum + pageSize;
        List<TblSensirionLog> sensirionLogs = sensirionLogService.selectByTime(pageNum, pageSize, gatewayMac, deviceMac, Time.stampToDate(start), Time.stampToDate(end));

        List<TblSensirionLogDTO> sensirionLogDTOS = new ArrayList<>();
        for (TblSensirionLog tblSecurityLog : sensirionLogs) {
            TblSensirionLogDTO tblSensirionLogDTO = new TblSensirionLogDTO();
            BeanUtils.copyProperties(tblSecurityLog, tblSensirionLogDTO);
            sensirionLogDTOS.add(tblSensirionLogDTO);
        }
        return R.ok(sensirionLogDTOS);

    }

    /**
     * 查询该设备当天数据
     */
    @GetMapping("/today")
    public R selectToday(@RequestParam("deviceMac") String deviceMac,
                         @RequestParam("gatewayMac") String gatewayMac) {

        List<TblSensirionLog> sensirionLogs = sensirionLogService.selectByToday(gatewayMac, deviceMac);

        List<TblSensirionLogDTO> sensirionLogDTOS = new ArrayList<>();
        for (TblSensirionLog sensirionLog : sensirionLogs) {
            TblSensirionLogDTO sensirionLogDTO = new TblSensirionLogDTO();
            BeanUtils.copyProperties(sensirionLog, sensirionLogDTO);
            sensirionLogDTOS.add(sensirionLogDTO);
        }

        return R.ok(sensirionLogDTOS);

    }

    /**
     * 查询该设备某天数据
     */
    @GetMapping("/day")
    public R selectByDay(@RequestParam("deviceMac") String deviceMac,
                         @RequestParam("gatewayMac") String gatewayMac,
                         @RequestParam("time") long time) {
        List<TblSensirionLog> tblSecurityLogs = sensirionLogService.selectByDay(gatewayMac, deviceMac, time);

        List<TblSensirionLogDTO> tblSecurityLogDTOS = new ArrayList<>();
        for (TblSensirionLog sensirionLog : tblSecurityLogs) {
            TblSensirionLogDTO sensirionLogDTO = new TblSensirionLogDTO();
            BeanUtils.copyProperties(sensirionLog, sensirionLogDTO);
            tblSecurityLogDTOS.add(sensirionLogDTO);
        }
        return R.ok(tblSecurityLogDTOS);

    }

}
